<?php
if (!defined('INDEX')) exit;
require("common/functions_jcas_modules.php");
$cfg['files_per_page'] = 10;
?>
<table width="100%"  border="0" cellspacing="0" cellpadding="2">
  <tr>
    <td class="datatd" bgcolor="#FFFFFF"><?php echo $lang['upload_desc']?></td>
  </tr>
</table>
<br />
<?php
####### ACTIONS ########
// Delete file
echo file_delete();
// Delete selected files
echo file_delete_selected();

if($show_content)
{
switch(@$_GET["p"])
{
	case "show":
	default:
		$upload_url = '../'.$cfg['upload_folder'];
		if($_GET["dir"])
			$upload_url .= $_GET["dir"].'/';
		$fo = @opendir($upload_url);
		?>

		<table width="100%"  border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
		<form name="frm_show" method="post" action="<?php echo $index_url?>&p=show">
			<tr>
				<td colspan="20" class="headtd"><?php echo $lang["uploads_title"]?></td>
			</tr>
			<tr align="center">
				<td width="20"  class="subheadtd"><?php echo $lang['order']?></td>
				<td  class="subheadtd"><?php echo $lang['icon']?></td>
				<td class="subheadtd"><?php echo $lang['file_name']?></td>
				<td  class="subheadtd"><?php echo $lang['size']?></td>
				<td  class="subheadtd"><?php echo $lang['last_access_date']?></td>
				<td class="subheadtd"><?php echo $lang['last_modify_date']?></td>
				<td  class="subheadtd"><input type="checkbox" name="all_id" class="box" value="all" onClick="set_all_cb('frm_show','delete_file[]','all_id');" title="<?php echo $lang['check_uncheck_all']?>"></td>
			</tr>
			<?php
			// Count total files
			$num_file = 0;
			while (($fr = readdir($fo)) !== false)
			{
				// Put file into array
				if(strlen($fr) >=4) 
				{
					$file_name[$num_file] = $fr;
					$num_file +=1;
				}
			}
			
			// Display files
			$no = 0;
			$start_for = ($_GET['pg']-1)*$cfg['files_per_page'];
			$end_for = $start_for + $cfg['files_per_page'];
			if($end_for>=$num_file) $end_for = $num_file;
			
			if( $_GET['dir'] ) { 
				$dir = $_GET['dir'].'/';
			}else{
				$dir='';
			}
			
			for($x=$start_for;$x<$end_for;$x++)
			{
				$no += 1;
				if(is_dir($upload_url.$file_name[$x])):
					$file['size'] = '';
				else:
					$file['size'] = number_format((filesize($upload_url.$file_name[$x])/1024)). ' KB';
				endif;
				$file['last_access'] = show_date($cfg['date_format'],fileatime($upload_url.$file_name[$x]));
				$file['last_modify'] = show_date($cfg['date_format'],filemtime($upload_url.$file_name[$x]));

				?>
					<tr onMouseOver="rollon(this);" onMouseOut="rolloff(this);">
						<td align="right" class="datatd"><?php echo ($_GET["pg"]-1)*$cfg["files_per_page"] +$no?></td>
						<td align="center" class="datatd"><?php echo file_show_icon($upload_url.$file_name[$x])?></td>
						<?php if (is_dir($upload_url.$file_name[$x])):?>
							<td class="datatd"><a href="admin.php?mdl=jcas_upload&SID=<?php echo session_id()?>&dir=<?php echo $dir.$file_name[$x]?>"><?php echo $file_name[$x]?></a></td>
						<?php else:?>
							<td class="datatd"><a href="<?php echo $upload_url.$file_name[$x]?>" target="_blank"><?php echo $file_name[$x]?></a></td>
						<?php endif;?>
						<td class="datatd" align="right"><font color="blue"><?php echo $file['size']?></font></td>
						<td align="center" class="datatd"><?php echo $file['last_access']?></td>
						<td align="center" class="datatd"><?php echo $file['last_modify']?></td>
						<td align="center" class="datatd">
						
						<a href="<?php echo $index_url?>&dir=<?php echo $dir?>&delete=<?php echo $dir.$file_name[$x]?>" onclick="return confirm('<?php echo $lang['js_confirm_delete'] ?>')" title="<?php echo $lang['truncate_this_table']?>"><img src="images/icon/icon_delete.gif" width="11" height="13" /></a>
						<input type="checkbox" name="delete_file[]" value="<?php echo $dir.$file_name[$x] ?>" class="box" />
						</td>
				  	</tr>
				<?php
			}
			if($num_file==0)
			{
				?>
				<tr>
					 <td colspan="20" align="center" class="datatd"><?php echo message($lang['no_requested_data'],'info')?></td>
				  </tr>
				<?php
			}
			?>
			
			<tr>
				 <td colspan="20" class="row1td" align="right"><input type="submit" name="delete_selected" value="<?php echo $lang['btn_delete_selected']?>" class="button" onclick="return confirm('<?php echo $lang['js_confirm_delete_selected']?>')"></td>
			</tr>
			<tr>
				<td colspan="20" align="center" class="datatd"><?php echo show_pages($num_file,$cfg['files_per_page'],$index_url.'&p='.get('p'))?></td>
			</tr>
			<input type="hidden" name="session_id" value="<?php echo session_id()?>">
		  </form>
		</table>
		<?php
		break;
		
		case "add":
		?>
		<table width="100%"  border="0" cellspacing="0" cellpadding="0" align="center">
				<form action="<?php echo $index_url?>&p=add" method="post" enctype="multipart/form-data" name="frm_add" onSubmit="return validate_frm_add();">
				  <tr>
					 <td colspan="2" class="headtd"><?php echo $lang['upload_new_file']?></td>
				  </tr>
				  <tr >
					 <td width="30%" class="row1td" valign="top"><b><?php echo $lang['upload_new_file']?> </b>*
					 </td>
				  <td width="70%" class="row2td" >
				  <input type="file" name="file" style="width:80%" /><br/>
				  <font class="info"><strong><?php echo $lang['allowed']?></strong>: <?php echo $cfg['allowed_file_type']?>. <strong>MAX:</strong> <?php echo $cfg['max_file_size']?> KB</font></td>
				  </tr>
				  
				  <tr>
					 <td colspan="2" align="center" valign="top" class="row2td">
					  	<input type="submit" name="insert" class="button" value="<?php echo $lang['btn_insert']?>">
			 	    </td>
				  </tr>
					 <input type="hidden" name="session_id" value="<?php echo session_id()?>">
		  </form>
</table>
			<?php
	break;
}// end switch
}// end show_content
$_GET["p"] = false;
?>